package com.demo.jpa.service;

import com.demo.jpa.dao.entity.UserEntity;
import com.demo.jpa.dao.entity.UserProcedureEntity;
import com.demo.jpa.dao.repository.UserProcedureRepository;
import lombok.extern.log4j.Log4j2;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.EntityManager;
import javax.persistence.ParameterMode;
import javax.persistence.StoredProcedureQuery;
import java.util.List;

/**
 * @Author:LiaoYouLe
 * @Date: Created in 2018/7/25
 * @Description
 * @Modifyied By:
 */
@Log4j2
public class ProcedureTry extends TestCase {
    @Autowired
    private UserProcedureRepository userProcedureRepository;
    @Autowired
    private EntityManager entityManager;


    @Test
    public void tryProcedureByProx() {
        Integer age = userProcedureRepository.queryAge(55);
        log.info("年龄 {}", age);
    }

    @Test
    public void tryProcedureByEntityManager() {
        StoredProcedureQuery procedureQuery = entityManager
                .createStoredProcedureQuery("query_user", UserProcedureEntity.class)
                .registerStoredProcedureParameter("arg", Integer.class, ParameterMode.IN);
        procedureQuery.setParameter("arg", 55);
        boolean execute = procedureQuery.execute();
        Assert.assertTrue(execute);
        procedureQuery.getResultList().forEach(log::info);
    }
}
